<!DOCTYPE html>
<html class="" lang="zh-CN">

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>中国人类遗传资源样本信息管理平台</title>
    <meta charset="utf-8">
    <meta content="always" name="referrer">
    <meta content="Chrome=1,IE=edge" http-equiv="X-UA-Compatible">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
</head>

<body>
<div class="moreBtnBox">
    <div class="importBox">
        <input type="file" multiple="multiple" id="importInput" class="importInput" accept=".csv">
        <div class="viewBtn" id="viewBtn">导入</div>
    </div>
    <div class="exportBox">
        <div class="viewBtn" id="exportBox">导出</div>
    </div>
</div>
<script src="js/jquery.min.js" charset="utf-8"></script>
<script src="js/downLoadCSV.js"></script>
<script src="js/papaparse.js"></script>
<script>
  //单选全选-导出
  var exportArrData = new Map();//需要导出的数据
  var exportArrDataId = [];//需要导出数据的id
  var exportArrDataNewId = [];//当前页数据id-是否全选
  //导出检索结果-csv表格
  var isDownLoadCSV = true;//防止重复点击-导出检索结果-csv表格
  // var $listSizeMax = 100;//用于导出数据，请求数据条数，列表最多展示长度
  var $downLoadCSVData = [];//csv表格-data
  var $downLoadCSVHeads = [];//csv表格-head
  var $downLoadCSVColumns = [];//csv表格-列
  var $downLoadCSVCSpeColumn = [];
  //导出检索结果-csv表格
  $(document).on('click', '#exportBox', function () {
    var exportArrDataSize = exportArrData.size();
    // var $Tips = exportArrDataSize > 50 ? '，需要等待较长时间' : '';
    console.log('exportArrData', exportArrData, exportArrDataId);
    if (isDownLoadCSV) {
      if (totalElements > 0) {
        if (confirm('下载检索结果，共计' + exportArrDataSize + '条')) {
          isDownLoadCSV = false;//防止重复点击-导出检索结果-csv表格
          console.log('download');
          var $listType = $('#serch-list-tbody').attr('data-selectOption');//列表当前类型
          wosDownLoadCSV(exportArrDataSize);//导出检索结果-document
        }
      } else {
        alert('当前没数据可供下载！');
      }
    } else {
      alert('正在下载检索结果！');
    }
  });
  //上传文件
  var importJsonResult = [];//上传文件-存放数据
  $('#viewBtn').on('click', function () {
    let files = $('#importInput')[0].files;
    if (files.length > 1) {
      alert('同时上传' + files.length + '文件');
    } else if (files.length == 0) {
      alert('上传文件不能为空！');
      return false;
    }
    $.each(files, function (index, val) {
      Papa.parse(val, {
        encoding: 'gbk',
        complete: function (results) {
          console.log(results.data);
          importJsonResult = [];
          importJsonResult = results.data;
          getFileDataFix(importJsonResult, val.name);//单文件上传，数据处理
        }
      });
    });
  });
  //导出检索结果-wos
  function wosDownLoadCSV($totalElements) {
    $downLoadCSVData = [];//csv表格-data
    $downLoadCSVHeads = [];//csv表格-head
    $downLoadCSVColumns = [];//csv表格-列
    var $DownLoadCSVAjaxPage = 0;
    var $heads = $('.tableBox thead tr th');
    // 导出检索结果-csv表格
    // for (var i = 0; i < $heads.length - 1; i++) {
    //   $downLoadCSVHeads[i] = $heads.eq(i).attr('indexImport');
    //   $downLoadCSVColumns[i] = $heads.eq(i).attr('indexImport');
    // }
    var wosStr = [
      // "id",
      "content",
      "category_id",
      "pdf_id",
      "start_pos",
      "end_pos",
    ];//数组
    $downLoadCSVHeads = $downLoadCSVColumns = wosStr;
    // var $stepNum = Math.ceil($totalElements / $listSizeMax);
    var $stepLoop = 0;//步数循环请求
    resultListRepeat($stepLoop);//分页请求列表结果
    function resultListRepeat($stepLoop) {
      $.each(exportArrData.container, function (index, val) {
        var $data1 = $downLoadCSVColumns[0];
        var $data2 = $downLoadCSVColumns[1];
        var $data3 = $downLoadCSVColumns[2];
        var $data4 = $downLoadCSVColumns[3];
        var $data5 = $downLoadCSVColumns[4];
        // var $data9 = $downLoadCSVColumns[8];
        var $obj = {};
        // $obj[$data1] = '"' + (isEmpty(val.id)).toString() + '"';
        $obj[$data1] = '"' + filterAjaxJsonToHtml(isEmptyMessage(val.content)) + '"';
        $obj[$data2] = '"' + filterAjaxJsonToHtml(isEmptyMessage(val.category_id)) + '"';
        $obj[$data3] = '"' + filterAjaxJsonToHtml(isEmptyMessage(val.pdf_id)) + '"';
        $obj[$data4] = '"' + filterAjaxJsonToHtml(isEmptyMessage(val.start_pos)) + '"';
        $obj[$data5] = '"' + filterAjaxJsonToHtml(isEmptyMessage(val.end_pos)) + '"';
        $downLoadCSVData.push($obj);//csv表格-data
      })
      isDownLoadCSV = true;//防止重复点击-导出检索结果-csv表格
      console.log('$downLoadCSVData', $downLoadCSVData)
      downLoadCSVDataReady();//导出检索结果-csv表格-数据ready
    }
    // }
  };
  //导出检索结果-csv表格-数据ready
  function downLoadCSVDataReady() {
    var $fileName = 'span.csv';//name
    data2csv({
      'data': $downLoadCSVData,
      'heads': $downLoadCSVHeads,
      'columns': $downLoadCSVColumns,
      'spe_column': $downLoadCSVCSpeColumn,
      'remove_targets': true,
    }, function (content) {
      // console.log(content);
      // download_file('order_list.csv',content,true)
      console.log('ready');
      download_file($fileName, content, true);
      $('#downLoadCSVProgress').removeClass('active');
    });
  };
  //单文件上传，数据处理
  function getFileDataFix(jsonArr, filesName) {
    var wosStrArr = [];//数组
    var jsonNameArr = [];//存放字段名称
    jsonArr.forEach(function (val, index) {
      if (index == 0) {
        // jsonNameArr = val;
        $.each(val, function (i, v) {
          jsonNameArr.push(v.replace(' ', ''))
        });
      } else if (index > 0 && val[0] !== '') {
        var wosStr = {
          "id": 0,
          "content": isEmptyMessage(val[jsonNameArr.indexOf('content')]),
          "categoryId": isEmptyMessage(val[jsonNameArr.indexOf('category_id')]),
          "pdfId": isEmptyMessage(val[jsonNameArr.indexOf('pdf_id')]),
          "startPos": isEmptyMessage(val[jsonNameArr.indexOf('start_pos')]),
          "endPos": isEmptyMessage(val[jsonNameArr.indexOf('end_pos')]),
        };//数组
        // wosStr.content = val[1];//content
        // wosStr.categoryId = val[2];//categoryId
        // wosStr.pdfId = val[3];//pdfId
        // wosStr.startPos = val[4];//startPos
        // wosStr.endPos = val[5];//endPos
        wosStrArr.push(wosStr);
      }
    });
    console.log('wosStrArr', wosStrArr);
    spanBatchImport(wosStrArr).then(function (data) {
      if (data.status == 'STATUS_SUCCESS') {
        // ShowMsg(filesName + '导入成功！');
        alert(filesName + '导入成功!');
        spanHtml();//获取数据类别列表分页
      } else {
        alert(filesName + '导入失败！');
      }
    }).fail(function () {
      alert(filesName + '导入失败！');
    });// 批量导入Wos
  };
</script>
</body>

</html>